<script setup>
import { ref, onMounted } from 'vue'
import { okxApi } from '../api/index.js'

// 要显示的交易对
const TRADING_PAIRS = ['BTC-USDT', 'ETH-USDT', 'SOL-USDT', 'DOT-USDT']

const prices = ref([])
const loading = ref(true)
const error = ref(null)

// 获取所有交易对的行情
const fetchAllPrices = async () => {
  try {
    loading.value = true
    error.value = null
    
    // 使用封装的 API 批量获取行情
    const results = await okxApi.getBatchTickers(TRADING_PAIRS)
    
    // 格式化数据
    prices.value = results
      .map(result => okxApi.formatTickerData(result))
      .filter(item => item !== null)
    
    if (prices.value.length === 0) {
      error.value = '无法获取行情数据'
    }
  } catch (err) {
    console.error('获取行情失败:', err)
    error.value = '获取行情失败，请检查后端服务是否运行'
  } finally {
    loading.value = false
  }
}

// 组件挂载时获取数据，并每5秒更新一次
onMounted(() => {
  fetchAllPrices()
  
  // 定时更新价格
  setInterval(() => {
    fetchAllPrices()
  }, 1000)
})
</script>

<template>
  <section class="price-section">
    <div class="container">
      <!-- 加载状态 -->
      <div v-if="loading && prices.length === 0" class="loading-state">
        <div class="spinner"></div>
        <p>正在加载行情数据...</p>
      </div>
      
      <!-- 错误状态 -->
      <div v-else-if="error" class="error-state">
        <p>{{ error }}</p>
        <button @click="fetchAllPrices" class="retry-btn">重试</button>
      </div>
      
      <!-- 价格卡片 -->
      <div v-else class="price-cards">
        <div 
          v-for="item in prices" 
          :key="item.pair"
          class="price-card"
        >
          <div class="price-pair">{{ item.pair }}</div>
          <div class="price-value">{{ item.priceFormatted }}</div>
          <div 
            class="price-change"
            :class="{ positive: item.isPositive, negative: !item.isPositive }"
          >
            {{ item.changeFormatted }}
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<style scoped>
.price-section {
  padding: 40px 0;
  background: white;
  border-radius: 20px;
  margin: -40px auto 0;
  max-width: 1200px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
  position: relative;
  z-index: 10;
}

.container {
  padding: 0 24px;
}

.price-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 24px;
}

.price-card {
  padding: 24px;
  background: linear-gradient(135deg, #f8f9fa 0%, #ffffff 100%);
  border-radius: 12px;
  border: 1px solid #e8e8e8;
  transition: all 0.3s;
}

.price-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  border-color: #00d4aa;
}

.price-pair {
  font-size: 14px;
  color: #666;
  font-weight: 600;
  margin-bottom: 8px;
}

.price-value {
  font-size: 28px;
  font-weight: 700;
  color: #1a1d29;
  margin-bottom: 8px;
  font-family: 'Courier New', monospace;
}

.price-change {
  font-size: 14px;
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 6px;
  display: inline-block;
}

.price-change.positive {
  color: #00d4aa;
  background: rgba(0, 212, 170, 0.1);
}

.price-change.negative {
  color: #ff4757;
  background: rgba(255, 71, 87, 0.1);
}

/* 加载状态 */
.loading-state {
  text-align: center;
  padding: 60px 20px;
}

.spinner {
  width: 40px;
  height: 40px;
  margin: 0 auto 16px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #00d4aa;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-state p {
  color: #666;
  font-size: 14px;
}

/* 错误状态 */
.error-state {
  text-align: center;
  padding: 60px 20px;
}

.error-state p {
  color: #ff4757;
  margin-bottom: 16px;
  font-size: 14px;
}

.retry-btn {
  padding: 10px 24px;
  background: #00d4aa;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s;
}

.retry-btn:hover {
  background: #00b894;
  transform: translateY(-2px);
}

@media (max-width: 768px) {
  .price-section {
    margin-top: 0;
  }
  
  .price-cards {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
  
  .price-card {
    padding: 16px;
  }
  
  .price-value {
    font-size: 20px;
  }
}
</style>
